/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package kimo.persistence.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import kimo.persistence.entities.RawProductType;
import kimo.persistence.entities.Production;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import kimo.persistence.controllers.exceptions.IllegalOrphanException;
import kimo.persistence.controllers.exceptions.NonexistentEntityException;
import kimo.persistence.entities.RawProduct;

/**
 *
 * @author a381u296
 */
public class RawProductJpaController implements Serializable {

    public RawProductJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(RawProduct rawProduct) {
        if (rawProduct.getProductionList() == null) {
            rawProduct.setProductionList(new ArrayList<Production>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            RawProductType rawProductTypeId = rawProduct.getRawProductTypeId();
            if (rawProductTypeId != null) {
                rawProductTypeId = em.getReference(rawProductTypeId.getClass(), rawProductTypeId.getRawProductTypeId());
                rawProduct.setRawProductTypeId(rawProductTypeId);
            }
            List<Production> attachedProductionList = new ArrayList<Production>();
            for (Production productionListProductionToAttach : rawProduct.getProductionList()) {
                productionListProductionToAttach = em.getReference(productionListProductionToAttach.getClass(), productionListProductionToAttach.getProductionId());
                attachedProductionList.add(productionListProductionToAttach);
            }
            rawProduct.setProductionList(attachedProductionList);
            em.persist(rawProduct);
            if (rawProductTypeId != null) {
                rawProductTypeId.getRawProductList().add(rawProduct);
                rawProductTypeId = em.merge(rawProductTypeId);
            }
            for (Production productionListProduction : rawProduct.getProductionList()) {
                RawProduct oldProductionRawProductIdOfProductionListProduction = productionListProduction.getProductionRawProductId();
                productionListProduction.setProductionRawProductId(rawProduct);
                productionListProduction = em.merge(productionListProduction);
                if (oldProductionRawProductIdOfProductionListProduction != null) {
                    oldProductionRawProductIdOfProductionListProduction.getProductionList().remove(productionListProduction);
                    oldProductionRawProductIdOfProductionListProduction = em.merge(oldProductionRawProductIdOfProductionListProduction);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(RawProduct rawProduct) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            RawProduct persistentRawProduct = em.find(RawProduct.class, rawProduct.getRawProductId());
            RawProductType rawProductTypeIdOld = persistentRawProduct.getRawProductTypeId();
            RawProductType rawProductTypeIdNew = rawProduct.getRawProductTypeId();
            List<Production> productionListOld = persistentRawProduct.getProductionList();
            List<Production> productionListNew = rawProduct.getProductionList();
            List<String> illegalOrphanMessages = null;
            for (Production productionListOldProduction : productionListOld) {
                if (!productionListNew.contains(productionListOldProduction)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Production " + productionListOldProduction + " since its productionRawProductId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (rawProductTypeIdNew != null) {
                rawProductTypeIdNew = em.getReference(rawProductTypeIdNew.getClass(), rawProductTypeIdNew.getRawProductTypeId());
                rawProduct.setRawProductTypeId(rawProductTypeIdNew);
            }
            List<Production> attachedProductionListNew = new ArrayList<Production>();
            for (Production productionListNewProductionToAttach : productionListNew) {
                productionListNewProductionToAttach = em.getReference(productionListNewProductionToAttach.getClass(), productionListNewProductionToAttach.getProductionId());
                attachedProductionListNew.add(productionListNewProductionToAttach);
            }
            productionListNew = attachedProductionListNew;
            rawProduct.setProductionList(productionListNew);
            rawProduct = em.merge(rawProduct);
            if (rawProductTypeIdOld != null && !rawProductTypeIdOld.equals(rawProductTypeIdNew)) {
                rawProductTypeIdOld.getRawProductList().remove(rawProduct);
                rawProductTypeIdOld = em.merge(rawProductTypeIdOld);
            }
            if (rawProductTypeIdNew != null && !rawProductTypeIdNew.equals(rawProductTypeIdOld)) {
                rawProductTypeIdNew.getRawProductList().add(rawProduct);
                rawProductTypeIdNew = em.merge(rawProductTypeIdNew);
            }
            for (Production productionListNewProduction : productionListNew) {
                if (!productionListOld.contains(productionListNewProduction)) {
                    RawProduct oldProductionRawProductIdOfProductionListNewProduction = productionListNewProduction.getProductionRawProductId();
                    productionListNewProduction.setProductionRawProductId(rawProduct);
                    productionListNewProduction = em.merge(productionListNewProduction);
                    if (oldProductionRawProductIdOfProductionListNewProduction != null && !oldProductionRawProductIdOfProductionListNewProduction.equals(rawProduct)) {
                        oldProductionRawProductIdOfProductionListNewProduction.getProductionList().remove(productionListNewProduction);
                        oldProductionRawProductIdOfProductionListNewProduction = em.merge(oldProductionRawProductIdOfProductionListNewProduction);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = rawProduct.getRawProductId();
                if (findRawProduct(id) == null) {
                    throw new NonexistentEntityException("The rawProduct with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            RawProduct rawProduct;
            try {
                rawProduct = em.getReference(RawProduct.class, id);
                rawProduct.getRawProductId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The rawProduct with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<Production> productionListOrphanCheck = rawProduct.getProductionList();
            for (Production productionListOrphanCheckProduction : productionListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This RawProduct (" + rawProduct + ") cannot be destroyed since the Production " + productionListOrphanCheckProduction + " in its productionList field has a non-nullable productionRawProductId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            RawProductType rawProductTypeId = rawProduct.getRawProductTypeId();
            if (rawProductTypeId != null) {
                rawProductTypeId.getRawProductList().remove(rawProduct);
                rawProductTypeId = em.merge(rawProductTypeId);
            }
            em.remove(rawProduct);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<RawProduct> findRawProductEntities() {
        return findRawProductEntities(true, -1, -1);
    }

    public List<RawProduct> findRawProductEntities(int maxResults, int firstResult) {
        return findRawProductEntities(false, maxResults, firstResult);
    }

    private List<RawProduct> findRawProductEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(RawProduct.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public RawProduct findRawProduct(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(RawProduct.class, id);
        } finally {
            em.close();
        }
    }

    public int getRawProductCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<RawProduct> rt = cq.from(RawProduct.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
